package com.situ.springboot.controller;

import com.situ.springboot.pojo.entity.Admin;
import com.situ.springboot.pojo.entity.Custom;
import com.situ.springboot.pojo.entity.User;
import com.situ.springboot.pojo.vo.PasswordVo;
import com.situ.springboot.service.IUserService;
import com.situ.springboot.util.PageInfo;
import com.situ.springboot.util.Result;
import org.apache.xmlbeans.impl.xb.xsdschema.Attribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;


    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id, Model model) {
        User user = userService.selectById(id);
        model.addAttribute("user", user);
        return "/user/update";
    }

    @RequestMapping("/update")
    public String update(User user) {
        userService.update(user);
        return "redirect:/user/selectByPage";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "/user/add";
    }

    @RequestMapping("/add")
    public String add(User user) {
        if(Objects.equals(user.getPassword(), "") || user.getPassword()==null){
            user.setPassword("123456");
        }
        userService.add(user);
        return "redirect:/user/selectByPage";
    }


    // /user/deleteById?id=23
    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        userService.deleteById(id);
        //redirect:重定向，告诉浏览器发送请求 /user/selectAll
        //删除完之后，应该查找展示最新的数据
        return "redirect:/user/selectByPage";
        //return "/user/selectAll";
    }

    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] ids) {
        userService.deleteAll(ids);
        return "redirect:/user/selectByPage";
    }

    @RequestMapping("/selectByPage")
    public String selectByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "10") Integer pageSize, Model model) {
        System.out.println("UserController.selectByPage");
        PageInfo pageInfo = userService.selectByPage(pageNo, pageSize);
        System.out.println("pageInfo:" + pageInfo);
        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        //转发到user_list界面展示
        return "/user/list";
    }

    @RequestMapping("/selectAll")
    public String selectAll(Model model) {
        // soutm
        System.out.println("UserController.selectAll");
        List<User> list = userService.selectAll();

        //把list数据放到内存里面
        model.addAttribute("list", list);
        //转发到user_list界面展示
        return "/user/list";
    }

    // @ResponseBody 返回json格式数据
    // json集合表示: [{'id':1,'name':'zhansgan'},{}]
    // json对象: {'id':1,'name':'zhansgan'}
    // http://localhost:8080/user/selectAll
    @RequestMapping("/selectAll1")
    @ResponseBody
    public List<User> selectAll1() {
        // soutm
        System.out.println("UserController.selectAll");
        List<User> list = userService.selectAll();

        return list;
    }

    @RequestMapping("/toUserInformation")
    public String showUserInformation(HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        User user1 = userService.selectById(id);
        model.addAttribute("user",user1);
        return "/user/user_information";
    }

    @RequestMapping("/UpdatePassword")
    public String UpdatePassword(HttpSession session, Model model, PasswordVo passwordVo){
        User user1 = (User) session.getAttribute("user");
        Integer id = user1.getId();
        User user= userService.selectById(id);
        if(Objects.equals(passwordVo.getPassword(), "")
                || Objects.equals(passwordVo.getPassword1(), "")
                || Objects.equals(passwordVo.getPassword2(), "")){
            return "index_normal";
        }
        else {
            user.setPassword(passwordVo.getPassword1());
            userService.update(user);
            return "redirect:/user/logout";
        }
    }
}
